<template>
  <view>
    <!-- 选择收货地址的盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(address)==='{}'">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
    </view>
    <!-- 渲染收货信息的盒子 -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="row1-left">
          <view class="username">
            收货人：{{address.userName}}
          </view>
        </view>
        <view class="row1-right">
          <view class="phone">
            电话：{{address.telNumber}}
          </view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>

      <view class="row2">
        <view class="row2-left">
          收货地址：
        </view>
        <view class="row2-right">
          {{addstr}}
        </view>
      </view>
    </view>
    <image src="/static/cart_border@2x.png" class="address-border"></image>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'

  export default {
    name: "my-address",
    data() {
      return {
        localAddress: {} // 本地维护的地址副本
      };
    },
    computed: {
      // 从 Vuex 获取地址（只读）
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['addstr']),
      // 本地地址计算属性（用于模板渲染）
      localAddressComputed() {
        return this.localAddress
      },
      // 优化：地址是否为空的计算属性
      isAddressEmpty() {
        return Object.keys(this.address).length === 0
      },
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),

      async chooseAddress() {
        try {
          const res = await uni.chooseAddress()
          if (res.errMsg === 'chooseAddress:ok') {
            // 1. 更新本地副本（使用解构避免直接引用）
            this.localAddress = {
              userName: res.userName,
              telNumber: res.telNumber,
              provinceName: res.provinceName,
              cityName: res.cityName,
              countyName: res.countyName,
              detailInfo: res.detailInfo
            }

            // 2. 提交到 Vuex（同步操作）
            this.updateAddress(this.localAddress)

            console.log('本地地址:', this.localAddress)
            console.log('Vuex 地址:', this.$store.state.m_user.address)
          }
        } catch (err) {
          console.log('地址选择失败:', err)
        }
      }
    },
    // 页面创建时初始化本地地址
    created() {
      // 从 Vuex 初始化本地数据（可选）
      if (Object.keys(this.address).length > 0) {
        this.localAddress = {
          ...this.address
        }
      }
    }
  }
</script>

<style lang="scss">
  .address-border {
    display: block;
    width: 100%;
    height: 5px;
  }

  .address-choose-box {
    display: flex;
    height: 90px;
    justify-content: center;
    align-items: center;
  }

  .address-info-box {
    font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
  }

  .row1 {
    display: flex;
    justify-content: space-between;
  }

  .row1-right {
    display: flex;
    justify-content: space-between;
  }

  .row2 {
    display: flex;
    align-items: center;
    margin-top: 10px;
  }

  .row2-left {
    white-space: nowrap;
  }
</style>